package com.neu.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class ExcelReader {

    public static List<Float> readHistory(String fileUrl){
        List<Float> historyPDRList = new ArrayList<>();

        URL url = null;
        try {
            url = new URL(fileUrl);
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            // 使用 EasyExcel 读取 Excel 文件
            EasyExcel.read(bufferedInputStream, ReadData.class, new PageReadListener<ReadData>(dataList -> {
                for (ReadData data : dataList) {
                    // 假设 ReadData 类中有一个名为 historyPDR 的字段
                    historyPDRList.add(data.getHistoryPDR());
                }
            })).sheet().doRead();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return historyPDRList;

    }

//    public static void main(String[] args) {
//        String fileName = "http://152.136.42.110:9115/swpt/1731640296214_history.xlsx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=UpqNh8wlUi48ZZAs4ARB%2F20241115%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20241115T031136Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=c0f9d55bc65ee2dc94eb24d551c149b0492a06e136e91f48acbf6f9f6de9015a"; // Excel 文件路径
//        List<Float> history = readHistory(fileName);
//        System.out.println(history);
//    }

    // 定义一个内部类来映射 Excel 的列
    public static class ReadData {
        private Float historyPDR;

        // 确保有一个无参构造方法
        public ReadData() {
        }

        // 确保有 getter 和 setter 方法
        public Float getHistoryPDR() {
            return historyPDR;
        }

        public void setHistoryPDR(Float historyPDR) {
            this.historyPDR = historyPDR;
        }
    }
}

